package com.mycjj.android.activity;

/**
 * 车圈 - 我的话题列表 BY baiyuxiong @linewin.cc
 */
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.CarNetworkThemeAdapter;
import com.mycjj.android.bean.CarNetworkCircleExtBean;
import com.mycjj.android.bean.CarNetworkThemesBean;
import com.mycjj.android.bean.CarNetworkThemesCircleBaseBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshListView;
import com.mycjj.android.pullrefresh.PullToRefreshBase.Mode;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener2;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.util.Utils;

public class CarNetworkMyDetailActivity extends BaseActivity {
	
	private PullToRefreshListView carnetwork_my_theme_list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_network_my_detail);
		
		init();
	}
	
	private void init(){
		initHeader();
		initData(0);
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back_selector);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("我的话题");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CarNetworkMyDetailActivity.this.finish();
			}
		});
		
		mHeaderRightButton.setVisibility(View.GONE);
		
		carnetwork_my_theme_list = (PullToRefreshListView)findViewById(R.id.carnetwork_my_theme_list);
		carnetwork_my_theme_list.setMode(Mode.BOTH);
		carnetwork_my_theme_list.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				Toast.makeText(CarNetworkMyDetailActivity.this, "下拉刷新", Toast.LENGTH_LONG).show();
				initData(0);
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				if (has_next_page) {// 有第二页数据
					index++;
					initData(index);
				} else {
					Toast.makeText(CarNetworkMyDetailActivity.this, "没有更多的数据需要显示!",
							Toast.LENGTH_LONG).show();
					carnetwork_my_theme_list.onRefreshComplete();
				}
			}

		});
		
		carnetwork_my_theme_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(CarNetworkMyDetailActivity.this,
						CarNetworkThemeDetailActivity.class);
				if(null != networkthemeList)
				{
					CarNetworkThemesBean bean = networkthemeList.get(position);
					intent.putExtra("id", bean.getTheme_id());
				}
				startActivity(intent);
			}
		});
	}
	
	private boolean has_next_page;
	private int total = 1;
	private int index = 0;
	private int limit = 8;
	
	List<CarNetworkThemesBean> networkthemeList;
	private CarNetworkThemeAdapter networkthemeadapter;
	
	private void initData(int index) {
		networkthemeadapter = new CarNetworkThemeAdapter(CarNetworkMyDetailActivity.this);

		//车圈
		ProjectHttpRequestInterface request = new ProjectHttpRequestImpl(
				CarNetworkMyDetailActivity.this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		String storeid = Preferences.getSharedPreferences(CarNetworkMyDetailActivity.this,
				"storeId", "");
		params.put("store_id", storeid);
		params.put("type", "my");
		params.put("token", Preferences.getSharedPreferences(CarNetworkMyDetailActivity.this,
				"user_token", ""));
		params.put("offset", String.valueOf(limit*index));
		params.put("limit", String.valueOf(limit));
		request.requestgetsMyThemesWithDoneHandler("", params,
			new AsyncHttpResponseHandler() {
				@Override
				public void onSuccess(String content) {
					CarNetworkThemesCircleBaseBean activityBean = ProjectDataManage
							.getInstance().getCarNetworkThemesCircleBaseBean();
					Log.d("CarNetWorkFragment",
							(null == activityBean) ? "CarNetworkBaseBean is null"
									: "activityBean code"
											+ activityBean.getCode());
					if ((null != activityBean)
							&& (null != activityBean.getData())) {
						has_next_page = activityBean.getData().isHas_next_page();
						total = activityBean.getData().getTotal();
						networkthemeList = activityBean.getData().getList();
						Log.i("byx", "networkthemeList size : " + networkthemeList.size());
						networkthemeadapter.setList(networkthemeList);
					}
					carnetwork_my_theme_list.onRefreshComplete();
				}

			});

		carnetwork_my_theme_list.setAdapter(networkthemeadapter);
	}
}
